package com.hzjq.encrypt.ui

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hzjq.encrypt.AbsActivity
import com.hzjq.encrypt.R
import com.hzjq.encrypt.bean.EnBean
import kotlinx.android.synthetic.main.activity_lg_list_layout.*
import org.greenrobot.eventbus.EventBus

abstract class AbsListActivity<T> : AbsActivity() {

    open lateinit var data: EnBean

    open lateinit var mAdapter : BaseQuickAdapter<T,BaseViewHolder>

    override fun onCreated() {
        lgListView.layoutManager = LinearLayoutManager(this)
        if (intent.hasExtra("data")) {
            data = intent.getSerializableExtra("data") as EnBean
        } else {
            data = EnBean()
        }

        onDataCreated()

        lgListView.adapter = mAdapter

        showEmpty()

        setHandle()
    }


    abstract fun onDataCreated()

    abstract fun onRemoveItemClick(position:Int)

    abstract fun onItemEditorClick(position:Int)

    abstract fun onAddNewClick()

    private fun setHandle(){
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.lgItemDeleteText -> {
                    onRemoveItemClick(position)
                }
                R.id.lgItemEditorText -> {
                    onItemEditorClick(position)
                }
            }
        }

        setOnMoreClick(View.OnClickListener {
            onAddNewClick()
        })

    }

    open fun notifyDataChanged(){
        mAdapter.notifyDataSetChanged()
        showEmpty()
        EventBus.getDefault().post(data)
    }

    private fun showEmpty() {
        lgListEmpty.visibility =
            if (mAdapter.data == null || mAdapter.data.isEmpty()) View.VISIBLE else View.GONE
        lgListView.visibility =
            if (mAdapter.data == null || mAdapter.data.isEmpty()) View.GONE else View.VISIBLE
    }

    override fun getContentViewId(): Int {
        return R.layout.activity_lg_list_layout
    }

    override fun getShowTitle(): String {
        return ""
    }
}